home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / BlobMgr / Blob Manager Demo 5 / TextDlog.c < prev   
Encoding:
C/C++ Source or Header  |  1987-02-17  |  4.4 KB  |  205 lines  |  [TEXT/MACA]

  1. /*
  2.     Present a dialog box with a text rectangle, a scroll bar, and an OK
  3.     button.  This can be used as a help dialog.  Pass in a dialog
  4.     resource number and a handle to the text.  The dialog should
  5.     have the OK button as the first item, and user items for the
  6.     second and third items.  The second is the rect in which the
  7.     control is drawn, the third is the rect in which text is drawn.
  8.     Only the OK button should be enabled.
  9.  
  10.     The text handle should be locked before calling TextDlog and
  11.     unlocked after.
  12. */
  13.  
  14. # include    <EventMgr.h>
  15. # include    <DialogMgr.h>
  16. # include    <ControlMgr.h>
  17.  
  18. # define    nil        0L
  19.  
  20. enum        /* item numbers */
  21. {
  22.     okBut = 1,
  23.     scrollBarUser,
  24.     textRectUser
  25. };
  26.  
  27. static ControlHandle    theScroll;
  28. static Boolean            needScroll;    /* true if need scroll bar */
  29. static int                curLine;
  30. static int                halfPage;
  31. static TEHandle            teHand;
  32.  
  33.  
  34. /*
  35.     Proc for drawing scroll bar user item.
  36. */
  37.  
  38. static pascal void DrawScroll (theDialog, itemNo)
  39. DialogPtr    theDialog;
  40. int            itemNo;
  41. {
  42.     if (needScroll)
  43.         ShowControl (theScroll);
  44. }
  45.  
  46.  
  47. /*
  48.     Proc for drawing text display user item.
  49. */
  50.  
  51. static pascal void DrawTextRect (theDialog, itemNo)
  52. DialogPtr    theDialog;
  53. int            itemNo;
  54. {
  55. Rect    r;
  56. int        itemType;
  57. Handle    itemHandle;
  58.  
  59.     GetDItem (theDialog, textRectUser, &itemType, &itemHandle, &r);
  60.     if (needScroll)
  61.         FrameRect (&r);
  62.     r = (**teHand).viewRect;
  63.     TEUpdate (&r, teHand);
  64. }
  65.  
  66.  
  67. /*
  68.     Scroll to the correct position.  lDelta is the
  69.     amount to CHANGE the current scroll setting by.
  70. */
  71.  
  72. static DoScroll (lDelta)
  73. int        lDelta;
  74. {
  75. int    newLine;
  76.  
  77.     newLine = curLine + lDelta;
  78.     if (newLine < 0) newLine = 0;
  79.     if (newLine > GetCtlMax (theScroll)) newLine = GetCtlMax (theScroll);
  80.     SetCtlValue (theScroll, newLine);
  81.     lDelta = (curLine - newLine ) * (**teHand).lineHeight;
  82.     TEScroll (0, lDelta, teHand);
  83.     curLine = newLine;
  84. }
  85.  
  86.  
  87. static pascal void __TrackScroll (theScroll, partCode)
  88. ControlHandle    theScroll;
  89. int                partCode;
  90. {
  91. int    lDelta;
  92.  
  93.     if (partCode == GetCRefCon (theScroll))    /* still in same part? */
  94.     {
  95.         switch (partCode)
  96.         {
  97.             case inUpButton: lDelta = -1; break;
  98.             case inDownButton: lDelta = 1; break;
  99.             case inPageUp: lDelta = -halfPage; break;
  100.             case inPageDown: lDelta = halfPage; break;
  101.         }
  102.         DoScroll (lDelta);
  103.     }
  104. }
  105.  
  106.  
  107. /*
  108.     Filter to handle hits in scroll bar
  109. */
  110.  
  111. static pascal Boolean TextFilter (theDialog, theEvent, itemHit)
  112. DialogPtr    theDialog;
  113. EventRecord    *theEvent;
  114. int            *itemHit;
  115. {
  116. Point    thePoint;
  117. int        thePart;
  118.  
  119.     if (theEvent->what == mouseDown)
  120.     {
  121.         thePoint = theEvent->where;
  122.         GlobalToLocal (&thePoint);
  123.         thePart = TestControl (theScroll, thePoint);
  124.         if (thePart == inThumb)
  125.         {
  126.             (void) TrackControl (theScroll, thePoint, nil);
  127.             DoScroll (GetCtlValue (theScroll) - curLine);
  128.         }
  129.         else if (thePart != 0)
  130.         {
  131.             SetCRefCon (theScroll, (long) thePart);
  132.             (void) TrackControl (theScroll, thePoint, __TrackScroll);
  133.         }
  134.     }
  135.     return (false);
  136. }
  137.  
  138.  
  139. TextDialog (dlogNum, textHandle, fontNum, fontSize, wrap)
  140. int        dlogNum;
  141. Handle    textHandle;
  142. int        fontNum;
  143. int        fontSize;
  144. Boolean    wrap;
  145. {
  146. GrafPtr        oldPort;
  147. DialogPtr    theDialog;
  148. int            itemNo;
  149. int            itemType;
  150. Handle        itemHandle;
  151. Rect        r;
  152. int            scrollLines;
  153. int            viewLines;
  154. Handle        oldHText;
  155. ProcPtr        filterProc = nil;
  156.  
  157.     GetPort (&oldPort);
  158.     theDialog = GetNewDialog (dlogNum, nil, -1L);
  159.     GetDItem (theDialog, textRectUser, &itemType, &itemHandle, &r);
  160.     SetDItem (theDialog, textRectUser, itemType, DrawTextRect, &r);
  161. /*
  162.     incorporate text into a TERec.
  163. */
  164.     SetPort (theDialog);
  165.     TextFont (fontNum);
  166.     TextSize (fontSize);
  167.     InsetRect (&r, 4, 4);
  168.     teHand = TENew (&r, &r);
  169.     if (!wrap)
  170.         (**teHand).crOnly = -1;
  171.     oldHText = (**teHand).hText;        /* save this.  restore later */
  172.     (**teHand).hText = textHandle;
  173.     TECalText (teHand);
  174.     viewLines = (r.bottom - r.top) / (**teHand).lineHeight;
  175.     scrollLines = (**teHand).nLines - viewLines;
  176.     needScroll = (scrollLines > 0);
  177.     GetDItem (theDialog, scrollBarUser, &itemType, &itemHandle, &r);
  178.     SetDItem (theDialog, scrollBarUser, itemType, DrawScroll, &r);
  179.     if (needScroll)
  180.     {
  181.         theScroll = NewControl (theDialog, &r, "\p", false, 0, 0, 0,
  182.                         scrollBarProc, nil);
  183.         SetCtlMax (theScroll, scrollLines);
  184.         halfPage = viewLines / 2;
  185.         curLine = 0;
  186.         filterProc = (ProcPtr) TextFilter;
  187.     }
  188.  
  189.     ShowWindow (theDialog);
  190.     /*do {*/
  191.         ModalDialog (filterProc, &itemNo);
  192.     /*} while (itemNo != okBut);*/
  193.  
  194. /*
  195.     restore hText field of TE record before disposing of it.
  196. */
  197.     (**teHand).hText = oldHText;
  198.     TEDispose (teHand);
  199.     /*ReleaseResource (textHandle);*/
  200.     if (needScroll)
  201.         DisposeControl (theScroll);
  202.     SetPort (oldPort);
  203.     DisposDialog(theDialog);
  204. }
  205.